//
//  AppDelegate.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+BaiduPush.h"
#import <SMS_SDK/SMSSDK.h>
#import "BPush.h"
#import <PgyUpdate/PgyUpdateManager.h>

#import "Reachability.h"
#import "BIDTransitionViewController.h"
#import "BIDManageViewController.h"

/**
 *网络状态改变事件
 */
NSString* const NETWORKSTATECHANGE = @"networkStateChange";

/**
 *当前网络是否已连接
 */
static BOOL bNetworkConnecting = YES;

@interface AppDelegate ()
{
    Reachability *_hostReach;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self baiduApplication:application didFinishLaunchingWithOptions:launchOptions];
    //短信验证码
    [SMSSDK registerApp:@"1b32ff5e3e332" withSecret:@"42867e8686db061e9c0dfd848bf9466d"];
    //清空角标数量
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //监听网络
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    NSString *hostName = @"www.apple.com";
    _hostReach = [Reachability reachabilityWithHostName:hostName];
    [_hostReach startNotifier];
    //蒲公英
    [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"ca9b66272618e1795920ced4a20bee0a"];
    //
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    BIDTransitionViewController *vc = [[BIDTransitionViewController alloc] init];
    vc.view.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
    navController.navigationBarHidden = YES;
    [self setNavigationControllerAttribute:navController];
    self.window.rootViewController = navController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}

/**
 *设置UINavigationController的属性
 */
- (void)setNavigationControllerAttribute:(UINavigationController *)navController
{
    [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBg.png"] forBarMetrics:UIBarMetricsDefault];
    NSDictionary *attributeDictionary = @{NSFontAttributeName:[UIFont systemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor whiteColor]};
    [navController.navigationBar setTitleTextAttributes:attributeDictionary];
    [navController.navigationBar setTintColor:[UIColor whiteColor]];
}

/**
 *网络状态监听
 */
- (void)reachabilityChanged:(NSNotification *)note
{
    Reachability *curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    int status = 0;
    if(netStatus == NotReachable)
    {
        status = 0;
        bNetworkConnecting = NO;
    }
    else
    {
        status = 1;
        bNetworkConnecting = YES;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NETWORKSTATECHANGE object:self userInfo:@{@"state":[NSNumber numberWithInt:status]}];
}
/**
 *  获取网络状态
 *
 *  @return <#return value description#>
 */
+ (BOOL)isNetworkConnecting
{
    return  bNetworkConnecting;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
